关于python:连接到远程IPython实例

您所在的位置:网站首页 ipython3 kernel 关于python:连接到远程IPython实例

关于python:连接到远程IPython实例

#关于python:连接到远程IPython实例 | 来源: 网络整理| 查看: 265

我想在一台机器上运行一个IPython实例,并通过另一个进程(运行一些python命令)(通过LAN)连接到它。 我了解使用zmq是可能的:http://ipython.org/ipython-doc/dev/development/ipythonzmq.html。

但是,我找不到有关如何执行操作以及是否可行的文档。

任何帮助,将不胜感激!

编辑

我希望能够连接到IPython内核实例并将其发送给python命令。 但是,这不应该通过图形工具(qtconsole)完成,但是我希望能够从不同的python脚本中连接到该内核实例。

例如

external.py

12somehow_connect_to_ipython_kernel_instance instance.run_command("a=6") 相关讨论 相关:stackoverflow.com/questions/18146558/

如果要从另一个Python程序在内核中运行代码,最简单的方法是连接BlockingKernelManager。现在最好的例子是Paul Ivanov的vim-ipython客户端,或IPython自己的终端客户端。

要点:

ipython内核在IPYTHONDIR/profile_/security/kernel-.json中编写JSON连接文件,其中包含各种客户端连接和执行代码所必需的信息。 KernelManager是用于与内核进行通信(执行代码,接收结果等)的对象。 *

一个工作示例:

在外壳程序中,执行ipython kernel(如果要与已经运行的GUI共享内核,请执行ipython qtconsole):

123$> ipython kernel [IPKernelApp] To connect another client to this kernel, use: [IPKernelApp] --existing kernel-6759.json

这写了'kernel-6759.json'文件

然后,您可以运行此Python代码段以连接KernelManager,并运行一些代码:

1234567891011121314151617181920212223242526272829303132333435from IPython.lib.kernel import find_connection_file from IPython.zmq.blockingkernelmanager import BlockingKernelManager # this is a helper method for turning a fraction of a connection-file name # into a full path.  If you already know the full path, you can just use that cf = find_connection_file('6759') km = BlockingKernelManager(connection_file=cf) # load connection info and init communication km.load_connection_file() km.start_channels() def run_cell(km, code):     # now we can run code.  This is done on the shell channel     shell = km.shell_channel     print     print"running:"     print code     # execution is immediate and async, returning a UUID     msg_id = shell.execute(code)     # get_msg can block for a reply     reply = shell.get_msg()     status = reply['content']['status']     if status == 'ok':         print 'succeeded!'     elif status == 'error':         print 'failed!'         for line in reply['content']['traceback']:             print line run_cell(km, 'a=5') run_cell(km, 'b=0') run_cell(km, 'c=a/b')

运行的输出:

1234567891011121314151617running: a=5 succeeded! running: b=0 succeeded! running: c=a/b failed! --------------------------------------------------------------------------- ZeroDivisionError                         Traceback (most recent call last) /Users/minrk/ in () ----> 1 c=a/b ZeroDivisionError: integer division or modulo by zero

有关如何解释回复的更多信息,请参见消息规范。如果相关,则stdout / err和显示数据将通过km.iopub_channel提供,并且您可以使用shell.execute()返回的msg_id将输出与给定执行关联。

PS:对于这些新功能的文档质量,我深表歉意。我们有很多工作要做。

相关讨论 非常感谢您的出色回答!正是我在寻找。是否可以接收在内核中运行的命令的输出?例如:" a = 2" .."打印a",然后接收" 2"? 是否可以调用IPython.frontend.terminal.embed.InteractiveShellEmbed()作为内核打开? 输出来自iopub_channel。正如我上面所链接的那样,消息规范详细说明了这些消息,并且vim-ipython和ipython控制台演示了如何处理这些消息。至于嵌入,我想您是在要求添加此内容,应该不久将其合并。 这对于IPython 2.2.0来说非常有用,我只需要用IPython.kernel.blocking.client.BlockingKernelClient替换BlockingKernelManager,相同的方法调用就可以了。谢谢!

如果您只想进行交互式连接,则可以使用SSH转发。我尚未在Stack Overflow的任何地方找到此文档,但是这个问题来得最接近。该答案已经在Ipython 0.13上进行了测试。我从这篇博客文章中获得了信息。

在远程计算机上运行ipython kernel:

123user@remote:~$ ipython3 kernel [IPKernelApp] To connect another client to this kernel, use: [IPKernelApp] --existing kernel-25333.json

查看kernel-25333.json文件:

123456789user@remote:~$ cat ~/.ipython/profile_default/security/kernel-25333.json {  "stdin_port": 54985,  "ip":"127.0.0.1",  "hb_port": 50266,  "key":"da9c7ae2-02aa-47d4-8e67-e6153eb15366",  "shell_port": 50378,  "iopub_port": 49981 }

在本地计算机上设置端口转发:

1234user@local:~$ ssh user@remote -f -N -L 54985:127.0.0.1:54985 user@local:~$ ssh user@remote -f -N -L 50266:127.0.0.1:50266 user@local:~$ ssh user@remote -f -N -L 50378:127.0.0.1:50378 user@local:~$ ssh user@remote -f -N -L 49981:127.0.0.1:49981

将kernel-25333.json文件复制到本地计算机:

1user@local:~$ rsync -av user@remote:.ipython/profile_default/security/kernel-25333.json ~/.ipython/profile_default/security/kernel-25333.json

使用新内核在本地计算机上运行ipython:

12345678910111213user@local:~$ ipython3 console --existing kernel-25333.json Python 3.2.3 (default, Oct 19 2012, 19:53:16) Type"copyright","credits" or"license" for more information. IPython 0.13.1.rc2 -- An enhanced Interactive Python. ?         -> Introduction and overview of IPython's features. %quickref -> Quick reference. help      -> Python's own help system. object?   -> Details about 'object', use 'object??' for extra details. In [1]: import socket; print(socket.gethostname()) remote 相关讨论 如果有人不知道json文件位于何处(像我一样):可以通过运行jupyter --runtime-dir找到它

拆分为Jupyter后更新为Minrk的答案。用 jupyter_client(4.1.1) 最简单的代码是这样的:

1234567import jupyter_client cf=jupyter_client.find_connection_file('6759') km=jupyter_client.BlockingKernelClient(connection_file=cf) km.load_connection_file() km.execute('a=5')

注意:

jupyter_client.BlockingKernelClient也使用jupyter_client.client.BlockingKernelClient作为别名。 外壳(km.shell_channel)不再具有execute()和get_msg()方法。

当前,很难找到更新的文档。对于BlockingKernelClient,http://jupyter-client.readthedocs.org/en/latest/上没有任何内容。 https://github.com/jupyter/jupyter_kernel_test中的一些代码。欢迎任何链接。

相关讨论 很好的答案!

上面的答案有点老了。最新版本ipython的解决方案要简单得多,但在一处没有得到很好的记录。所以我想在这里记录下来。

从任何操作系统连接到在Windows上运行的ipython内核的解决方案

如果客户端或服务器是linux或其他操作系统,只需根据Windows下Jupyter中的kernel-1234.json位于何处来适当地更改kernel-1234.json的位置。

在基于Windows的内核启动上,确保使用pip install ipykernel安装了ipykernel 使用ipython kernel -f kernel-1234.json启动ipykernel 在Windows计算机上找到kernel-1234.json文件。该文件可能具有不同的编号,而不是1234,并且很可能位于" C: Users me AppData roaming jupyter runtime kernel-1234.json"中:https://stackoverflow.com / a / 48332006/4752883 使用pip install jupyter-console或pip install qtconsole https://jupyter-console.readthedocs.io/en/latest/安装Jupyter Console(或Jupyter Qtconsole / notebook) 如果您在Windows上,请执行ipconfig来查找Windows服务器的IP地址。 (在Linux上,在shell提示符下输入ifconfig)。在kernel-1234.json文件中,将IP地址从127.0.0.1更改为服务器的IP地址。如果要从另一台Windows服务器连接,则将kernel-1234.json文件复制到本地计算机并记下路径。 导航到包含kernel-1234.json的文件夹,然后使用jupyter console --existing kernel-1234.json启动Jupyter Console。 相关讨论 在Windows安装中,我没有ipykernel,而是我有jupyter-kernel。当它开始时,它显示:Connection file: \kernel-.json。在此文件中,它显示为"ip":"127.0.0.1",因此我看不到如何在尝试使用该内核的另一台计算机上使用在客户端计算机上使用此文件?当我将127.0.0.1更改为运行jupyter-kernel的计算机的IP地址(例如10.0.0.122)时,Jupyter控制台每3秒重复一次kernel died。 是。你是对的。当我写这篇文章时,我是在同一台机器上运行客户端和服务器。实际上,当您运行Jupyter Notebook时,在同一台计算机上运行着一个客户端和一个服务器,这就是127.0.0.1正常运行的原因。如果必须在不同的计算机上运行客户端和服务器,则需要1.确保从客户端可见服务器ip。 2.在kernel-1234.json中,确保将IP更改为客户端设置的IP。 不是100%肯定,但是您提到的10.0.0.122 ip似乎不是一个可以从客户端看到的ip。您是否尝试使用ping 10.0.0.122 ping服务器以检查服务器是否从客户端可见?没有防火墙或什么东西阻止连接? 您在服务器和客户端上安装了什么? 是的,Ping有效。两台机器都在同一LAN上(10.0.x.x和192.168.x.x是公用的本地LAN地址)。最初的问题专门询问有关在LAN上的计算机上访问IPython的问题。 现在,我正在以网桥网络格式运行服务器和客户端,因此这确实适用于LAN连接。您还会注意到json文件中提到的多个端口。这些端口必须是可见/开放的。您是否在客户端和服务器上运行任何防火墙? 另外,在上述解决方案中,我还有一个错字,如果要启动ipython kernel,则需要使用相同的kernel-1234.json文件启动它。 kernel-1234.json文件应具有相同的IP地址。 在将文件与服务器ip一起使用并确保没有防火墙在运行时,您是否可以尝试使用ipykernel按照说明进行操作? 当我在服务器上执行pip list时,没有看到jupyter-kernel。 我也没有在这里看到它被列为内核:github.com/jupyter/jupyter/wiki/Jupyter-kernels。 当我尝试pip install jupyter-kernel时,它不会安装任何东西。 所以我不确定你在做什么。 谢谢,在服务器上,启动ipython kernel --ip 10.0.1.122(以明确指定其IP地址)会导致其创建的.json文件中具有该IP,而不是127.0.0.1),此外还会导致Windows防火墙提示打开该文件。 所需的端口。 然后,在客户端计算机上,使用相同的.json文件启动客户端即可。 似乎Python发行版才刚刚开始将IPython客户端部分重命名为Jupyter; 我的Anaconda dist兼有。 当我在客户端上启动ipython qtconsole时,它警告我应该开始使用jupyter qtconsole。 非常感谢!

如果您使用的是Anaconda,则在OS X中,JSON文件存储在

/Users/[username]/Library/Jupyter/runtime/

在Windows中:

c:\Users[username]\AppData oaming\jupyter untime\

相关讨论 ...和Windows上的c: Users用户名] AppData roaming jupyter runtime 这会回答问题吗? OP希望"在一台机器上运行IPython实例,并通过不同的进程(通过LAN)连接到它"。 @WaiHaLee,我认为它确实是解决问题的关键。对于初学者来说,kernel-1234.json文件的位置可能会令人困惑。有关使用最新版本的ipython / ipykernel的完整解决方案,请访问:stackoverflow.com/a/48332182/4752883



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3